package com.hongxun.design.mytools;

import android.content.Context;
import android.widget.Toast;

import com.hongxun.design.MyApp;

/**
 * Created by zerdoor_pc on 2016/3/18.
 */
public class ToastShow {

    private static String oldMsg;
    private static long time;
    static Toast toast;
    public static void showToast(Context context, String msg, int duration) {
        if (!msg.equals(oldMsg)) { // 当显示的内容不一样时，即断定为不是同一个Toast
            toast=Toast.makeText(context, msg, duration);
            toast.show();
            time = System.currentTimeMillis();
        } else {
            // 显示内容一样时，只有间隔时间大于2秒时才显示
            if (System.currentTimeMillis() - time > 2000) {
                toast= Toast.makeText(context, msg, duration);
                toast.show();
                time = System.currentTimeMillis();
            }
        }
        oldMsg = msg;
    }


    public synchronized static void showLong(Context context, String msg) {
        try {
            if (msg.length()==0)return;
            showToast(context, msg, Toast.LENGTH_SHORT);
            //Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        } catch (Exception e) {
        }
    }

    public synchronized static void showShort(Context context, String msg) {
        // if (msg.equals(MString.Connection))return;
        if (msg.length()==0)return;
        try {
            if (context != null) {
                showToast(context, msg, Toast.LENGTH_SHORT);
              //  Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            } else if (MyApp.activity != null) {
                showToast(MyApp.activity, msg, Toast.LENGTH_SHORT);
                //Toast.makeText(MyApp.activity, msg, Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
        }


    }

}
